package com.itdianzi.controller.export;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.export.Export;
import com.itdianzi.domain.export.ExportProduct;
import com.itdianzi.domain.export.ExportProductExample;
import com.itdianzi.domain.system.User;
import com.itdianzi.export_common.common.utils.BeanMapUtils;
import com.itdianzi.service.cargo.ExportProductService;
import com.itdianzi.service.cargo.ExportService;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/export")
public class ExportPDFController extends BaseController {
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;


    @RequestMapping("/exportPdf")

    public void exportPdf(String id) throws JRException, IOException {
        //查询报运单的数据
        Export export = exportService.findById(id);
        System.out.println(export);

        //查看报运单下货物的数据
        ExportProductExample exportProductExample = new ExportProductExample();
        //条件拼接对象
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);
        //拼接条件
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);
        //将javaBean转换成map类型数据
        Map<String, Object> map = BeanMapUtils.beanToMap(export);

        //将export.jasper加载到程序
        InputStream is = session.getServletContext().getResourceAsStream("jasper/export.jasper");

        //文件+数据 填充jasper文件数据
        JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);
        //填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, jrBeanCollectionDataSource);
        //打印下载
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());


    }


    @RequestMapping("/exportPdf6")
    public void exportPdf6() throws JRException, IOException {

        //加载文件到程序
        InputStream is = session.getServletContext().getResourceAsStream("jasper/test06.jasper");

        //创建list集合
        ArrayList<User> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {

                User user = new User();
                user.setUserName("名称" + j);
                user.setEmail("410847804@q.com" + j);
                user.setCompanyName("企业" + i);
                user.setDeptName("部门" + j);
                list.add(user);
            }
        }

        //构建jasper的集合数据
        JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);

        //填充数据
        /**
         * public static JasperPrint fillReport(InputStream inputStream,Map<String,Object> parameters,JRDataSource dataSource
         * 参数1: 文件的流
         * 参数2: map类型数据
         * 参数3: list类型数据
         */
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, new HashMap<>(), jrBeanCollectionDataSource);
        //下载文件
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());


    }


    @RequestMapping("/exportPdf5")
    public void exportPdf5() throws JRException, IOException {

        //加载文件到程序
        InputStream is = session.getServletContext().getResourceAsStream("jasper/test05.jasper");

        //创建list集合
        ArrayList<User> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUserName("名称" + i);
            user.setEmail("410847804@q.com" + i);
            user.setCompanyName("企业" + i);
            user.setDeptName("部门" + i);
            list.add(user);
        }

        //构建jasper的集合数据
        JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);

        //填充数据
        /**
         * public static JasperPrint fillReport(InputStream inputStream,Map<String,Object> parameters,JRDataSource dataSource
         * 参数1: 文件的流
         * 参数2: map类型数据
         * 参数3: list类型数据
         */
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, new HashMap<>(), jrBeanCollectionDataSource);
        //下载文件
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());


    }


    /**
     * 导出pdf 入门案例
     * 文件+ 数据 + 下载
     */
    @RequestMapping("/exportPdf1")
    public void exportPdf1() throws JRException, IOException {

        //加载文件到程序
        InputStream is = session.getServletContext().getResourceAsStream("jasper/test01.jasper");

        //创建map
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName", "王兵");
        map.put("email", "410847804@qq.com");
        map.put("companyName", "一条龙");
        map.put("deptName", "管理部");

        //填充数据
        /**
         * public static JasperPrint fillReport(InputStream inputStream,Map<String,Object> parameters,JRDataSource dataSource
         * 参数1: 文件的流
         * 参数2: map类型数据
         * 参数3: list类型数据
         */
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JREmptyDataSource());
        //下载文件
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());


    }


}
